1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import com.google.common.annotations.GwtIncompatible;
20 import com.google.common.base.Predicate;
21
22 import junit.framework.TestCase;
23
24 import java.util.Arrays;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28
29
30
31
32
33 @GwtIncompatible("nottested")
34 public class FilteredMultimapTest extends TestCase {
35
36 private static final Predicate<Map.Entry<String, Integer>> ENTRY_PREDICATE
37 = new Predicate<Map.Entry<String, Integer>>() {
38 @Override public boolean apply(Entry<String, Integer> entry) {
39 return !"badkey".equals(entry.getKey()) && !((Integer) 55556).equals(entry.getValue());
40 }
41 };
42
43 protected Multimap<String, Integer> create() {
44 Multimap<String, Integer> unfiltered = HashMultimap.create();
45 unfiltered.put("foo", 55556);
46 unfiltered.put("badkey", 1);
47 return Multimaps.filterEntries(unfiltered, ENTRY_PREDICATE);
48 }
49
50 private static final Predicate<String> KEY_PREDICATE
51 = new Predicate<String>() {
52 @Override public boolean apply(String key) {
53 return !"badkey".equals(key);
54 }
55 };
56
57 public void testFilterKeys() {
58 Multimap<String, Integer> unfiltered = HashMultimap.create();
59 unfiltered.put("foo", 55556);
60 unfiltered.put("badkey", 1);
61 Multimap<String, Integer> filtered = Multimaps.filterKeys(unfiltered, KEY_PREDICATE);
62 assertEquals(1, filtered.size());
63 assertTrue(filtered.containsEntry("foo", 55556));
64 }
65
66 private static final Predicate<Integer> VALUE_PREDICATE
67 = new Predicate<Integer>() {
68 @Override public boolean apply(Integer value) {
69 return !((Integer) 55556).equals(value);
70 }
71 };
72
73 public void testFilterValues() {
74 Multimap<String, Integer> unfiltered = HashMultimap.create();
75 unfiltered.put("foo", 55556);
76 unfiltered.put("badkey", 1);
77 Multimap<String, Integer> filtered = Multimaps.filterValues(unfiltered, VALUE_PREDICATE);
78 assertEquals(1, filtered.size());
79 assertFalse(filtered.containsEntry("foo", 55556));
80 assertTrue(filtered.containsEntry("badkey", 1));
81 }
82
83 public void testFilterFiltered() {
84 Multimap<String, Integer> unfiltered = HashMultimap.create();
85 unfiltered.put("foo", 55556);
86 unfiltered.put("badkey", 1);
87 unfiltered.put("foo", 1);
88 Multimap<String, Integer> keyFiltered = Multimaps.filterKeys(unfiltered, KEY_PREDICATE);
89 Multimap<String, Integer> filtered = Multimaps.filterValues(keyFiltered, VALUE_PREDICATE);
90 assertEquals(1, filtered.size());
91 assertTrue(filtered.containsEntry("foo", 1));
92 assertTrue(filtered.keySet().retainAll(Arrays.asList("cat", "dog")));
93 assertEquals(0, filtered.size());
94 }
95
96
97 }